#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#define PI 3.141592653

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    QCPItemLine *item = new QCPItemLine(ui->plot);
    item->setPen(QPen(Qt::red));
    item->start->setCoords(1,1);
    item->end->setCoords(2,3);
    QCPItemEllipse *circle = new QCPItemEllipse(ui->plot);
    circle->setPen(QPen(Qt::darkGreen));
    circle->topLeft->setCoords(5,0);
    circle->bottomRight->setCoords(10,5);
    ui->plot->replot();
    //画圆弧
    QVector <QPointF> myData(51);

    for(int j=0;j<51;j++)
    {
        myData[j].setX(2*cos(/*(PI/2)*(j/50))*/PI*j/100));
        myData[j].setY(2*sin(PI*j/100));
    }
    for(int j=0;j<50;j++)
    {
        QCPItemLine *item = new QCPItemLine(ui->plot);
        item->setPen(QPen(Qt::green));
        item->start->setCoords(myData[j].x(),myData[j].y());
        item->end->setCoords(myData[j+1].x(),myData[j+1].y());
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pb_g2plot_2_clicked()
{
    double stX,stY,edX,edY=0;
    bool gotX,gotY,gotI,gotJ = false;
    //清除图形
    ui->plot->clearItems();
    ui->plot->update();

    // 获取文本输入框的内容
    QString inputText = ui->textEdit->toPlainText();

    // 定义正则表达式提取G代码中的点线信息
    QRegularExpression regex("([GXYIJgxyij])([+-]?\\d+\\.?\\d*)");

    // 在文本中查找匹配的模式
    QRegularExpressionMatchIterator matchIter = regex.globalMatch(inputText);

    // 遍历匹配的结果
    while (matchIter.hasNext()) {
        QRegularExpressionMatch match = matchIter.next();
        if(match.captured(1)=='G'||match.captured(1)=='g')
        {
            if(!gotX)
            {
                stX = edX;
            }
            if(!gotY)
            {
                stY = edY;
            }
            if(!gotI)
            {
                i=0;
            }
            if(!gotJ)
            {
                j=0;
            }
            switch (mode) {
            case 1:
                drawLine(stX,stY,edX,edY);
                qDebug()<<stX<<stY<<edX<<edY;
                break;
            case 2:
                if(j>0)
                {
                    theta1 = atan(i/j)+PI;
                }
                else if(i>0&&j<0)
                {
                    theta1 = atan(i/j)+2*PI;
                }
                else if(i<=0&&j<0)
                {
                    theta1 = atan(i/j);
                }
                else if(i>0&&j==0)
                {
                    theta1 = 3*PI/2;
                }
                else if(i<0&&j==0)
                {
                    theta1 = PI/2;
                }
                i1 = stX+i-edX;
                j1 = stY+j-edY;
                if(j1>0)
                {
                    theta2 = atan(i1/j1)+PI;
                }
                else if(i1>0&&j1<0)
                {
                    theta2 = atan(i1/j1)+2*PI;
                }
                else if(i1<=0&&j1<0)
                {
                    theta2 = atan(i1/j1);
                }
                else if(i1>0&&j1==0)
                {
                    theta2 = 3*PI/2;
                }
                else if(i1<0&&j1==0)
                {
                    theta2 = PI/2;
                }
                if(theta2<=theta1)
                {
                    theta2 = theta2+2*PI;
                }
                drawArc(stX,stY,i,j,theta1,theta2);
                break;
            case 3:
                if(j>0)
                {
                    theta1 = -atan(i/j)+PI;
                }
                else if(i>=0&&j<0)
                {
                    theta1 = -atan(i/j);
                }
                else if(i<0&&j<0)
                {
                    theta1 = -atan(i/j)+2*PI;
                }
                else if(i>0&&j==0)
                {
                    theta1 = PI/2;
                }
                else if(i<0&&j==0)
                {
                    theta1 = 3*PI/2;
                }
                i1 = stX+i-edX;
                j1 = stY+j-edY;
                if(j1>0)
                {
                    theta2 = -atan(i1/j1)+PI;
                }
                else if(i1>=0&&j1<0)
                {
                    theta2 = -atan(i1/j1);
                }
                else if(i1<=0&&j1<0)
                {
                    theta2 = -atan(i1/j1)+2*PI;
                }
                else if(i1>0&&j1==0)
                {
                    theta2 = PI/2;
                }
                else if(i1<0&&j1==0)
                {
                    theta2 = 3*PI/2;
                }
                if(theta2<=theta1)
                {
                    theta2 = theta2+2*PI;
                }
                drawccwArc(stX,stY,i,j,theta1,theta2);
                break;
            default:
                break;
            }
            gotX=false;gotY=false;gotI=false;gotJ=false;
            mode = match.captured(2).toInt();
        }
        if(match.captured(1)=='X'||match.captured(1)=='x')
        {
            switch (mode)
            {
            case 0:
                stX = match.captured(2).toDouble();
                edX = stX;
                break;
            case 1:
                stX = edX;
                edX = match.captured(2).toDouble();
                break;
            case 2:
                stX = edX;
                edX = match.captured(2).toDouble();
                break;
            case 3:
                stX = edX;
                edX = match.captured(2).toDouble();
                break;
            }
            gotX = true;
        }
        if(match.captured(1)=='Y'||match.captured(1)=='y')
        {
            switch (mode) {
            case 0:
                stY = match.captured(2).toDouble();
                edY = stY;
                break;
            case 1:
                stY = edY;
                edY = match.captured(2).toDouble();
                break;
            case 2:
                stY = edY;
                edY = match.captured(2).toDouble();
                break;
            case 3:
                stY = edY;
                edY = match.captured(2).toDouble();
                break;
            }
            gotY = true;
        }
        if(match.captured(1)=='I'||match.captured(1)=='i')
        {
            i = match.captured(2).toDouble();
            gotI = true;
        }
        if(match.captured(1)=='J'||match.captured(1)=='j')
        {

            j = match.captured(2).toDouble();
            gotJ = true;
        }
    }
    ui->plot->replot();
}

void MainWindow::drawLine(double startx,double starty,double endx,double endy)
{
    QCPItemLine *item = new QCPItemLine(ui->plot);
    item->setPen(QPen(QColor(255,0,125)));
    item->start->setCoords(startx,starty);
    item->end->setCoords(endx,endy);
}

void MainWindow::drawArc(double startx,double starty,double i0,double j0,double theta1,double theta2)
{
    double radius = sqrt(i*i+j*j);
    QVector <QPointF> myData(51);

    for(int j=0;j<51;j++)
    {
        myData[j].setX(startx+i0+radius*sin(theta1+(theta2-theta1)*j/50));
        myData[j].setY(starty+j0+radius*cos(theta1+(theta2-theta1)*j/50));
    }
    for(int j=0;j<50;j++)
    {
        QCPItemLine *item = new QCPItemLine(ui->plot);
        item->setPen(QPen(QPen(QColor(255,0,125))));
        item->start->setCoords(myData[j].x(),myData[j].y());
        item->end->setCoords(myData[j+1].x(),myData[j+1].y());
    }
}

void MainWindow::drawccwArc(double startx,double starty,double i0,double j0,double theta1,double theta2)
{
    double radius = sqrt(i*i+j*j);
    QVector <QPointF> myData(51);

    for(int j=0;j<51;j++)
    {
        myData[j].setX(startx+i0-radius*sin(theta1+(theta2-theta1)*j/50));
        myData[j].setY(starty+j0+radius*cos(theta1+(theta2-theta1)*j/50));
    }
    for(int j=0;j<50;j++)
    {
        QCPItemLine *item = new QCPItemLine(ui->plot);
        item->setPen(QPen(QPen(QColor(255,0,125))));
        item->start->setCoords(myData[j].x(),myData[j].y());
        item->end->setCoords(myData[j+1].x(),myData[j+1].y());
    }
}

